<?php
/**
 * 订单处理服务类
 * Enter description here ...
 * @author Administrator
 *
 */
class OrderService {
	protected $shxx = array (); //获取收货信息
	protected $user = array ();
	//以下三个参数对应的是积分记录 而不是订单
	private $chargeType = array ("充值" => 0, "消费" => 1, "赠送" => 2 );
	private $payment = array ("收入" => 0, "支出" => 1 );
	private $status = array ("全部" => "0", "等待审核" => 1, "成功" => 8, "关闭" => - 1 );
	/**
	 * 产品类型 由系统检查而不是用户提交
	 * @var unknown_type
	 */
	private $_goodstype = array ("coupons", "money" );
	public function audit() {
		//统计用户总账
		$userCoupons = MemberModel::instance ()->getSumCouponsMember ();
		//获取期初余额
		$audit = OrderModel::instance ()->getLastAudit ();
		//获取所有支出 根据结算的sno号 已经确认
		$spend = OrderModel::instance ()->getLastSpend ( $audit ['coupon_no'], 8 );
		//所有支出  等待审核
		$unconfirmed = OrderModel::instance ()->getLastSpend ( $audit ['coupon_no'], 1 );
		$spendTotal = $spend ['spend'] + $unconfirmed ['spend'];
		//获取所有收入 根据结算的流水号
		$income = OrderModel::instance ()->getLastIncome ( $audit ['coupon_no'] );
		
		$shengyu = ($audit ['balance'] + $audit ['unconfirmed'] + $spendTotal + $income ['income']);
		$status = round ( $shengyu, 2 ) == round ( $userCoupons ['total'], 2 ) ? "ok" : "waring";
		//获取最老一笔未确认的订单
		$lastCouponsId = OrderModel::instance ()->getMinUnConfirmedId ();
		//修正当未确认为0的时候
		if ($lastCouponsId ['id'] < 1) {
			$lastCouponsId = OrderModel::instance ()->getMaxCoupons ();
		} else {
			$lastCouponsId ['id'] = $lastCouponsId ['id'] - 1;
		}
		return array ('unconfirmed' => abs ( $unconfirmed ['spend'] ), 'status' => $status, 'balance' => $userCoupons ['total'], 'spend' => abs ( $spend ['spend'] ), 'income' => $income ['income'], 'coupon_no' => $lastCouponsId ['id'] );
	}
	/**
	 * 新增结算
	 * Enter description here ...
	 * @param unknown_type $params
	 */
	public function setAudit($params) {
		
		if (! $this->checkAudit ( $params ['coupon_no'] )) {
			return "未确认订单之后有已经确认的订单，不允许结算";
		} else {
			OrderModel::instance ()->addAudit ( $params );
			return "结算成功";
		}
	
	}
	
	/**
	 * 如果未确认订单之后有已经确认的订单，不允许结算
	 * @return boolean true or false
	 */
	private function checkAudit($couponsId) {
		$rs = OrderModel::instance ()->getCouponsById ( $couponsId );
		return empty ( $rs );
	
	}
	/**
	 * 获取统计帐
	 * Enter description here ...
	 */
	public function getAudit($limit = 20) {
		return OrderModel::instance ()->getAudit ( $limit );
	}
	/**
	 * 获取所有用户信息
	 * Enter description here ...
	 */
	public function verifyCoupons() {
		//获取所有的用户id
		$user = MemberModel::instance ()->getCouponsMember ();
		foreach ( $user as $v ) {
			//     echo $v['uid'], " done! \n";
			$history = OrderModel::instance ()->getSumCouponsByUid ( $v ['uid'] );
			if ($history ['total'] - $v ['coupons'] != 0) {
				echo $v ['uid'], "历史", $history ['total'], "现有", $v ['coupons'], " not match <br>";
			}
		}
	
		//对其财务单明细匹配和现有的账户进行校验处理
	}
	public function getUserCoupons() {
	}
	/**
	 * 批量打印并且合并订单 可以直接修改返回值
	 * */
	public function getVerifyOrder() {
		return OrderModel::instance ()->getVerifyOrder ();
	}
}